<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              v-if="isAuth('module:business:information:import:download')"
              @click="downloadExcel"
              class="el-button--cyan"
              icon="Download">
              {{ $t('business.information.import.download') }}
            </el-button>
          </div>
          <div>
            <el-upload
              v-if="isAuth('module:business:information:import:handle')"
              :action="$http.adornUrl('/business/information/import/handle')"
              :show-file-list="false"
              :headers="upload_headers"
              :on-progress="beforeAttachmentUpload"
              :on-success="handleAttachmentSuccess">
              <el-button type="primary" icon="Upload">
                {{ $t('business.information.import.import') }}
              </el-button>
            </el-upload>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-input v-model="dataForm.title" :placeholder="$t('common.please_input') + $t('business.information.import.title')" clearable>
          </el-input>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.create_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
          </el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" label="#" width="70px"> </el-table-column>

          <el-table-column prop="title" :label="i18n('business.information.import.title')" min-width="200">
          </el-table-column>

          <el-table-column :label="i18n('business.information.import.remark')" width="140">
            <template v-slot="scope">
              <el-text type="danger" tag="b">
                {{ scope.row.remark }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('business.information.import.success_total')" width="120">
            <template v-slot="scope">
              <el-text type="primary" tag="b">
                {{ scope.row.success_total }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('business.information.import.error_total')" width="120">
            <template v-slot="scope">
              <el-text type="danger" tag="b">
                {{ scope.row.error_total }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('business.information.import.progress')" width="100">
            <template v-slot="scope">
              <el-text tag="b">
                {{ scope.row.progress.text }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('business.information.import.generator')" width="120">
            <template v-slot="scope">
              <el-text v-if="scope.row.user" type="primary">
                {{ scope.row.user.nickname }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column prop="create_time" :label="i18n('business.information.import.create_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="200">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:business:information:import:log:list')"
                icon="Tickets"
                @click="router.push({ name: 'module_business_information_import_log_list', query: {import_id: scope.row.id} })">
                {{ i18n('business.information.import.log.info') }}
              </el-button>

              <el-button
                v-if="isAuth('module:business:information:import:delete') && 0 != scope.row.progress.value"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="business-information-import-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/information/import')

  const refresh = ref(true)

  const dataForm = reactive([
    'title',
    'create_time',
  ])

  const {
    upload_headers,
  } = useUpload(dataForm, '')

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    handleStatus,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const beforeAttachmentUpload = (res, file) => {
    refresh.value = !refresh.value
  }

  const handleAttachmentSuccess = (res, file) => {
    getDataList(model)
    refresh.value = !refresh.value
  }

  const downloadExcel = () => {
    $http({
      url: $http.adornUrl('/business/information/import/download'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        window.location.href = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    getDataList(model)
  })
</script>
